package com.haisen.dataobject.query;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

public class RankDynaSqlProvider {

    public String selectWithRankSql(RankQuery rankQuery) {

        return new SQL() {
            {

                SELECT(" username,sum(core) as score ");
                FROM(" bs_rank ");
                if (!StringUtils.isEmpty(rankQuery.getSearchDate())) {
                    WHERE("cdate >= #{beginDate} ");
                    WHERE("cdate <= #{endDate}");
                }
                if(!StringUtils.isEmpty(rankQuery.getUsername())) {
                    WHERE("username=#{username} ");
                }
                GROUP_BY("username");
               // ORDER_BY(" desc");
            }
        }.toString();
    }


    public String selectCountWithRankSql(RankQuery rankQuery) {
        String temtable = selectWithRankSql(rankQuery);
        return "select count(*) from ("+temtable+") t ";

    }

}
